class SceneDescription(object):
    '''
    SceneDescription represents the data for a single 
    cutscene. 
    A cutscene can be viewed as a series of (text,image,time)
    tuples where [text] is intended to be viewed over [image]
    for a given [time], before the scene is advanced to the next
    tuple.
    '''
    
    textTupleIndex = 0;
    imgTupleIndex = 1;
    timeTupleIndex = 2;
    posTupleIndex = 3;
    longTextTupleIndex = 4;

    def advance(self):
        '''
        Advances the scene so that the text, image, and time
        are the next in the sequence. Returns True if successful
        Returns False if nothing remaining in the sequence;
        '''
        self.index += 1;
        if self.index >= len(self.content):
            self.END = True;
    
    @property
    def folder(self):
        return self._folder
    
    @property
    def background(self):
        return self._background
    
    @property
    def text(self):
        '''
        Returns current text
        '''
        if self.END:
            return None;
        return self.content[self.index][SceneDescription.textTupleIndex];
    
    @property
    def image(self):
        '''
        Returns current image
        '''
        if self.END:
            return None;
        return self.content[self.index][SceneDescription.imgTupleIndex];
    
    @property
    
    def isLongText(self):
        '''
        Returns if text should display as short or long text
        '''
        if self.END:
            return False;
        return self.content[self.index][SceneDescription.longTextTupleIndex];
    
    @property
    def position(self):
        '''
        returns one of Left, Right, Full corresponding 
        to display on bottom-left, bottom-right, or fullscreen
        '''
        if self.END:
            return None;
        return self.content[self.index][SceneDescription.posTupleIndex];
     
    @property   
    def time(self):
        '''
        Returns current time
        '''
        if self.END:
            return 0;
        return int(self.content[self.index][SceneDescription.timeTupleIndex])
    
    def append(self,text,image,time,position,long):
        '''
        Appends [tuple] to the scene
        '''
        self.content.append((text,image,time,position,long))
        self.END = (self.index >= len(self.content))
    
    def readFile(self, sceneFileName):  
        sf = open(sceneFileName);
        for line in sf:
            skip = False;
            if line[0] == "#":
                skip = True;
            if line.strip() == "":
                skip = True;
            if not skip and line.strip()[0:12]==".background=":
                self._background=line.strip()[12:];
                skip = True;
            if not skip and line.strip()[0:8]==".folder=":
                self._folder=line.strip()[8:];
                skip = True;
            if not skip:
                splitline = line.split("|")
                if len(splitline)<4:
                    raise Exception("invalid line: "+line)
                img = splitline[0].strip()
                if img == "$bg":
                    img = None
                time = self.defaultTime
                if not splitline[3].strip() == "$dt":
                    time = int(splitline[3].strip())
                text = "";
                long = False
                if splitline[2].strip()[0] == "$":
                    text=self.longStrings.getString(splitline[2].strip()[1:])
                    long = True
                else: 
                    text = splitline[2].strip()
                self.append(text, img, time,splitline[1].strip(),long)

    def __init__(self, sceneFileName, longStrings, defaultTime):
        '''
        Constructor
        '''
        self.content = [];
        self.defaultTime = defaultTime
        self.index = 0;
        self.END = False;  
        self._folder="";
        self._background="";
        self.longStrings = longStrings
        self.readFile(sceneFileName);    
        